
<template>
  <div>
      <div style="margin-bottom: 10px;">
        <el-input v-model="name" placeholder="请输入卡类型名称...."  suffix-icon="el-icon-search" style="width: 200px"></el-input>
        <el-button style="margin-left: 5px;" @click="loadPostByName">查询</el-button>
        <el-button @click="resetParam">重置</el-button>
        <el-button style="margin-left: 5px;" @click="add">新增</el-button>
      </div>

    <el-table :data="tableData"
              :header-cell-style="{background:'#eaecf3',color:'rgb(106,117,119)'}" border
    >
      <el-table-column prop="cardTypeId" label="卡类型ID" width="180">
      </el-table-column>
      <el-table-column prop="typeName" label="卡类型名称" width="180">
      </el-table-column>
      <el-table-column prop="discountRate" label="折扣率" width="180">
      </el-table-column>
      <el-table-column prop="minRecharge" label="最低充值金额" width="180">
      </el-table-column>
      <el-table-column prop="validityPeriod" label="有效期(月)" width="180">
      </el-table-column>
      <el-table-column prop="isActive" label="是否启用" width="180">
        <template slot-scope="scope">
          <el-tag
              disable-transitions>{{ scope.row.isActive ?'Ture':'False' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="operate" label="操作" width="260" >
        <!--可以访问当前行数据-->
        <template slot-scope="scope">
        <el-button @click="mod(scope.row)">编辑</el-button>
        <el-button @click="del(scope.row.cardTypeId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
    <el-dialog title="提示" :visible.sync="centerDialogVisible" width="30%" center>
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="卡类型名称" prop="typeName">
          <el-col :span="20">
            <el-input v-model="form.typeName"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="折扣率" prop="discountRate">
          <el-col :span="20">
          <el-input v-model="form.discountRate"> </el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="最低充值金额" prop="minRecharge">
          <el-col :span="20">
            <el-input v-model="form.minRecharge"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="有效期" prop="validityPeriod">
          <el-col :span="20">
            <el-input v-model="form.validityPeriod"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="是否启用" prop="isActive">
          <el-radio-group v-model="form.isActive">
            <el-radio label="true"></el-radio>
            <el-radio label="false"></el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'CardTypeManage',

  data() {
    return {
      tableData: [],
      pageSize:10,
      pageNum:1,
      total:0,
      name:'',
      centerDialogVisible: false,
      form:{
        cardTypeId:'',
        typeName:'',
        discountRate:'',
        minRecharge:'',
        validityPeriod:'',
        isActive:'',
      }
    }
  },
  methods:{
    resetForm() {
      this.$refs.form.resetFields();
    },
    mod(row){
      console.log(row)
      this.centerDialogVisible = true
      this.$nextTick(() => {
        this.form=row;
      })

    },
    add() {
      this.centerDialogVisible = true
      this.$nextTick(() => {
        this.resetForm()
      })
    },
    doSave(){
      this.$axios.post(this.$httpUrl + '/cardType/save',this.form).then(res => res.data).then(res => {
        console.log(res)
        if (res.code == 200) {
          this.$message({
            message: '操作成功！',
            type: 'success'
          });
          this.loadPost();
          this.centerDialogVisible = false
        } else {
          this.$message({
            message: '操作失败！',
            type: 'error'
          })
        }
      })
    },
    doMod(){
      this.$axios.post(this.$httpUrl + '/cardType/update',this.form).then(res => res.data).then(res => {
        console.log(res)
        if (res.code == 200) {
          this.$message({
            message: '操作成功！',
            type: 'success'
          });
          this.loadPost();
          this.centerDialogVisible = false
        } else {
          this.$message({
            message: '操作失败！',
            type: 'error'
          })
        }
      });
    },
    save(){
      this.$refs.form.validate((valid)=>{
        if(valid){
          if(this.form.cardTypeId){
            this.doMod();
          }else {
            this.doSave();
            }
          }else {
            console.log('error submit');
            return false;
          }
      });
    },
    del(cardTypeId) {
      console.log(cardTypeId);

      this.$axios.get(this.$httpUrl + '/cardType/delete?cardTypeId=' + cardTypeId)
          .then(res => res.data)
          .then(res => {
            console.log(res);
            if (res.code == 200) {
              this.$message({
                message: '操作成功！',
                type: 'success'
              });
              this.loadPost();
            } else {
              this.$message({
                message: '操作失败！',
                type: 'error'
              });
            }
          })
          .catch(error => {
            console.error('删除操作出错:', error);
            this.$message({
              message: '操作出错！',
              type: 'error'
            });
          });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum=1
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },
    loadPost(){
      this.$axios.get(this.$httpUrl+'/cardType/listPage',{
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize
        }
      }).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.tableData=res.data
          this.total=res.total
        }else {
          alert('获取数据失败')
        }

      })
    },
    loadPostByName(){
      this.$axios.get(this.$httpUrl+'/cardType/listPageByName',{
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name
        }
      }).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.tableData=res.data
          this.total=res.total
        }else {
          alert('获取数据失败')
        }

      })
    },
    resetParam(){
      this.name=''
      this.loadPost()
    }
  },
  beforeMount() {
    //this.loadGet();
    this.loadPost();
  }
}
</script>
<style scoped>

</style>